#include "user_task.h"
#include "led_UI.h"


//任务句柄
TaskHandle_t StartTask_Handler;//开始任务
TaskHandle_t GimbalTask_Handler;//云台任务
TaskHandle_t ShootTask_Handler;//发射任务
TaskHandle_t MonitorTask_Handler;//检测任务
TaskHandle_t TextTask_Handler;//
TaskHandle_t LED0Task_Handler;//
TaskHandle_t LED_UI_Task_Handler;//状态灯检测
char RunTimeInfo[400];
#define KEY_V_Q 0x40
#define KEY_V_E 0x80
#define KEY_V_W 0x01

//开始任务
void start_task(void *pvParameters)
{
    taskENTER_CRITICAL();        //进入临界区
	
	//创建Gimbal任务
    xTaskCreate((TaskFunction_t )gimbal_task,     
                (const char*    )"gimbal_task",   
                (uint16_t       )GIMBAL_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )GIMBAL_TASK_PRIO,
                (TaskHandle_t*  )&GimbalTask_Handler);
	//创建Shoot任务
    xTaskCreate((TaskFunction_t )shoot_task,     
                (const char*    )"shoot_task",   
                (uint16_t       )SHOOT_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )SHOOT_TASK_PRIO,
                (TaskHandle_t*  )&ShootTask_Handler);
	//创建Monitor任务
    xTaskCreate((TaskFunction_t )monitor_task,     
                (const char*    )"monitor_task",   
                (uint16_t       )MONITOR_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )MONITOR_TASK_PRIO,
                (TaskHandle_t*  )&MonitorTask_Handler);
								//LED_UI任务			
	xTaskCreate((TaskFunction_t )LED_UI_task,     
                (const char*    )"LED_UI_task",   
                (uint16_t       )LED_UI_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )LED_UI_TASK_PRIO,
                (TaskHandle_t*  )&LED_UI_Task_Handler);
   //创建Text任务
    xTaskCreate((TaskFunction_t )text_task,     
                (const char*    )"text_task",   
                (uint16_t       )TEXT_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )TEXT_TASK_PRIO,
                (TaskHandle_t*  )&TextTask_Handler); 

   xTaskCreate((TaskFunction_t )led0_task,     	
                (const char*    )"led0_task",   	
                (uint16_t       )LED0_STK_SIZE, 
                (void*          )NULL,				
                (UBaseType_t    )LED0_TASK_PRIO,	
                (TaskHandle_t*  )&LED0Task_Handler);   								
    vTaskDelete(StartTask_Handler); //删除开始任务
    taskEXIT_CRITICAL();            //退出临界区
}


void led0_task(void *pvParameters)
{
	  static uint8_t cnt;
    while(1)
    {
		    Tir_Task();
	//		#if HardwareType == Common_Hardware_Type
		  	cnt++;
			  if(cnt==200)
				{
				   	LED_Turn(LED_B);
					  cnt=0;
				}
	//			//这块加上状态灯
//			#elif HardwareType == Engineer_Hardware_Type
//			LED_Turn();	
//		  #endif
//	    printf("LED Turning\r\n");
				vTaskDelay(1);	
    }
}   


//Gimbal任务
void gimbal_task(void *pvParameters)
{
	taskENTER_CRITICAL();        //进入临界区
	
	//创建Gimbal_Update任务
    xTaskCreate((TaskFunction_t )gimbal_update_task,     
                (const char*    )"gimbal_update_task",   
                (uint16_t       )GIMBAL_UPDATE_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )GIMBAL_UPDATE_TASK_PRIO,
                (TaskHandle_t*  )&GimbalUpdateTask_Handler);
	//创建Gimbal_Calculate任务
    xTaskCreate((TaskFunction_t )gimbal_calculate_task,     
                (const char*    )"gimbal_calculate_task",   
                (uint16_t       )GIMBAL_CALCULATE_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )GIMBAL_CALCULATE_TASK_PRIO,
                (TaskHandle_t*  )&GimbalCalculateTask_Handler);
	//创建Gimbal_Ctrl任务
    xTaskCreate((TaskFunction_t )gimbal_ctrl_task,     
                (const char*    )"gimbal_ctrl_task",   
                (uint16_t       )GIMBAL_CTRL_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )GIMBAL_CTRL_TASK_PRIO,
                (TaskHandle_t*  )&GimbalCtrlTask_Handler);
	//创建Gimbal_Echo任务
    xTaskCreate((TaskFunction_t )gimbal_echo_task,     
                (const char*    )"gimbal_echo_task",   
                (uint16_t       )GIMBAL_ECHO_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )GIMBAL_ECHO_TASK_PRIO,
                (TaskHandle_t*  )&GimbalEchoTask_Handler);
	
	vTaskDelete(GimbalTask_Handler); 	//删除云台任务
    taskEXIT_CRITICAL();            	//退出临界区
}


//Shoot任务
void shoot_task(void *pvParameters)
{
	taskENTER_CRITICAL();        //进入临界区
	
	//创建Shoot_Ctrl任务
    xTaskCreate((TaskFunction_t )shoot_ctrl_task,
                (const char*    )"shoot_ctrl_task",   
                (uint16_t       )SHOOT_CTRL_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )SHOOT_CTRL_TASK_PRIO,
                (TaskHandle_t*  )&ShootCtrlTask_Handler);
	
	vTaskDelete(ShootTask_Handler); 	//删除发射任务
    taskEXIT_CRITICAL();            	//退出临界区
}


//Monitor任务
void monitor_task(void *pvParameters)
{
	taskENTER_CRITICAL();        //进入临界区
	
	//创建Gimbal_Monitor任务
    xTaskCreate((TaskFunction_t )gimbal_monitor_task,     
                (const char*    )"gimbal_monitor_task",   
                (uint16_t       )GIMBAL_MONITOR_TASK_SIZE, 
                (void*          )NULL,
                (UBaseType_t    )GIMBAL_MONITOR_TASK_PRIO,
                (TaskHandle_t*  )&GimbalMonitorTask_Handler);
	
	vTaskDelete(MonitorTask_Handler); 	//删除监控任务
    taskEXIT_CRITICAL();            	//退出临界区
}


//TEXT任务信息文本输出任务
void text_task(void *pvParameters)
{
    while(1)
    {
		memset(RunTimeInfo,0,400);				//信息缓冲区清零
		vTaskGetRunTimeStats(RunTimeInfo);		//获取任务运行时间信息
		printf("task_name\trunning_time\tpercent\r\n");
		printf("%s\r\n",RunTimeInfo);
		vTaskDelay(200);
    }
}
void LED_UI_task(void *pvParameters)
{
		while(1)
		{
			MY_LED_UI(0,0);
	    MY_LED_UI(1,0);
	    MY_LED_UI(2,0);
			LED_UI();
				
		
			vTaskDelay(1000);
		}
		
}
